package com.dxt.controller;

import com.dxt.pojo.User;
import com.dxt.service.UserService;
import com.dxt.service.UserServiceImpl;
import com.dxt.utils.MD5Util;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.SneakyThrows;

import java.io.IOException;

/**
 * BelongProject: deliveryManage
 * BelongPackage: com.dxt.controller
 * Description:
 *
 * @Author 段鑫涛
 * @Create 2025/3/30 23:01
 * @Version 1.0
 */
@WebServlet("/user/updatePassword")
public class UserUpdatePasswordController extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    @SneakyThrows
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String oldPassword = req.getParameter("oldPassword");
        String newPassword = req.getParameter("newPassword");

        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");
        Integer id = user.getId();

        if(MD5Util.encrypt(oldPassword).equals(user.getPassword())) {
            userService.UpdatePassword(newPassword,id);
            session.invalidate();
            resp.sendRedirect("/login.html");
        }else {
            req.getRequestDispatcher("/WEB-INF/updatePasswordError.html").forward(req, resp);
        }
    }
}
